package com.nxy.cpfsfe.busi.unfyag;

import com.adtec.starring.datapool.CompSDO;
import com.adtec.starring.datapool.EPOper;
import com.adtec.starring.exception.BaseException;
import com.adtec.starring.struct.dta.DtaInfo;
import com.adtec.starring.util.SpringUtil;
import com.nxy.cpfsfe.enumtype.RespCd;
import com.nxy.cpfsfe.enumtype.StatEnum;
import com.nxy.cpfsfe.enumtype.TsPCmdEnum;
import com.nxy.cpfsfe.mybatis.mapper.TsPCmdMapper;
import com.nxy.cpfsfe.mybatis.pojo.TsPCmd;
import com.nxy.cpfsfe.service.CpfsService;
import com.nxy.cpfsfe.util.DateUtil;
import com.nxy.cpfsfe.util.ResponseUtil;
import com.nxy.cpfsfe.util.StringUtil;
import com.nxy.cpfsfe.util.XmlUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;

/**
 * Description交易状态同步流程
 * Created by lcq on 2019/12/25.
 */
public class XStaQrySrvBean implements CpfsService {
    private final static Logger LOGGER = LoggerFactory.getLogger(XStaQrySrvBean.class);

    @Override
    public void doService() {
        LOGGER.debug("---------交易状态查询同步流程业务开始---------");
        Map<String,String> map = null;
        String rpsMsg ="";
        String tpId = DtaInfo.getInstance().getTpId();
        // 获取输入
        CompSDO inputSdo = (CompSDO) EPOper.getCompSDO(tpId, "ReqBody"); // 获取输入接口
        try {
            String MsgTp = (String)EPOper.get(tpId, "ReqBean[0].head[0].MsgTp");
            String bizMsg = (String) inputSdo.getValue("BizMsg")!=null?(String) inputSdo.getValue("BizMsg"):""; // 业务报文
            LOGGER.info("CPFS业务报文编号[{}],CPFS业务报文：[{}]",MsgTp,bizMsg);
            //生成业务map
            map = XmlUtil.xml2Map(bizMsg);
            //根据CPFS.200报文解析查询原交易信息,返回结果报文
            rpsMsg = handleXStaQry(map);

        }catch (BaseException e) {
            LOGGER.error("交易状态同步流程处理失败,错误码[{}],错误原因[{}]",e.getErrorCode(),e.getErrorDesc());
            LOGGER.error("错误信息栈",e);
            map.put("msg", e.getErrorDesc());
            map.put("resultCode",e.getErrorCode());
        }catch (Throwable e) {
            LOGGER.error("交易状态同步流程处理失败",e);
            map.put("msg", "交易状态同步流程处理失败");
            map.put("resultCode",RespCd.NCPO3999);
        }
        ResponseUtil.responseSend(rpsMsg,map);
        LOGGER.debug("---------转发类业务同步响应成功---------");
    }

    /**
     * 处理200查询报文，并返回501报文
     * @param map
     * @return
     */
    private String handleXStaQry(Map<String, String> map) {
        //报文检查
        String oriTrxId = map.get("/root/MsgBody/TrxInf/OriTrxInf/OriTrxId");
        if(StringUtils.isEmpty(oriTrxId)){
            throw new BaseException(RespCd.NCPO0119,"原交易流水号不能为空");
        }
        String bizTyp = map.get("/root/MsgBody/TrxInf/OriTrxInf/OriTrxCtgy");
//        if(StringUtils.isEmpty(bizTyp)){
//            throw new BaseException(RespCd.NCPO0119,"原交易类型不能为空");
//        }
        SqlSession mybatisSqlSession = (SqlSession) SpringUtil.getBean("mybatisSqlSession");
        TsPCmdMapper tsPCmdMapper = mybatisSqlSession.getMapper(TsPCmdMapper.class);
        //设置查询条件
        TsPCmd tsPCmdQo = new TsPCmd();
        tsPCmdQo.setBizSysD(DateUtil.str2Date(map.get("/root/MsgBody/TrxInf/OriTrxInf/OriTrxDtTm"),"yyyyMMddhhmmss"));
        tsPCmdQo.setXSeqNo(oriTrxId);
        tsPCmdQo.setBizTyp(StringUtil.trimObject(bizTyp));
        tsPCmdQo.setBizSysId(map.get("/root/MsgHeader/SndSysId"));
//        if(TsPCmdEnum.BizTyp_Integre.getCode().equals(bizTyp)||TsPCmdEnum.BizTyp_Integre.getCode().equals(bizTyp)){
//            //积分支付的话
//            tsPCmdQo.setAcqOrgId(map.get("/root/MsgHeader/RcvBankId"));
//        }else{
            tsPCmdQo.setAcqOrgId(map.get("/root/MsgHeader/SndBankId"));
//        }
        TsPCmd tsPCmd = tsPCmdMapper.query(tsPCmdQo);
        if(tsPCmd == null){
            throw new BaseException(RespCd.NCPS5005,"原交易信息不存在，请检查相关要素信息");
        }
        String bizSta = tsPCmd.getBizSta();
        String resultCode = RespCd.NCPO1114;
        String msg = "业务状态值非法";
        if(StatEnum.BIZSTA_SUC.getCode().equals(bizSta)){
            resultCode =RespCd.NCPI0000;
            msg = StatEnum.BIZSTA_SUC.getName();
        }else if(StatEnum.BIZSTA_FAIL.getCode().equals(bizSta)){
            resultCode =tsPCmd.getErrCd();
            msg =tsPCmd.getErrDesc();
            if(StringUtils.isEmpty(resultCode)){
                resultCode =RespCd.NCPO6804;
                msg = StatEnum.BIZSTA_FAIL.getName();
            }
        }else if(StatEnum.BIZSTA_ING.getCode().equals(bizSta)){
            resultCode =RespCd.NCPO9004;
            msg = StatEnum.BIZSTA_ING.getName();
        }else if(StatEnum.BIZSTA_INIT.getCode().equals(bizSta)){
            resultCode =RespCd.NCPO9004;
            msg = StatEnum.BIZSTA_INIT.getName();
        }
        map.put("msg",msg);
        map.put("resultCode",resultCode);
        return  ResponseUtil.build501Xml(map);

    }
}
